package com.itcast.framdemo.interceptor;/*
package com.itcast.framdemo.interceptor;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.itcast.framdemo.utils.JWTUtils;
import com.itcast.framdemo.utils.JsonResult;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import java.util.HashMap;
import java.util.Map;

*/

import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import java.util.HashMap;
import java.util.Map;

/**
 * @author WangCong
 * 2024/12/10 20:47
 */
/*

@Component
@Slf4j
public class JWTInterceptors implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Map<String,Object> map = new HashMap<>();
        // 获取请求头中令牌
        String token = request.getHeader("token");
        try {
            // 验证令牌
            log.info("开始校验jwt令牌:{}",token);
            boolean verify = JWTUtils.verify(token);
            if (verify){
                return true;  // 放行请求
            }else {
                map.put("code",0);
                map.put("data",null);
                map.put("msg","令牌无效!!!");
                // 将map以json的形式响应到前台  map --> json  (jackson)
                String json = new ObjectMapper().writeValueAsString(map);
                response.setContentType("application/json;charset=UTF-8");
                response.getWriter().println(json);
                return false;
            }
        } catch (Exception e) {
            map.put("code",0);
            map.put("data",null);
            map.put("msg","无效签名！");
        }
        return false;
    }

}
*/
